//
//  LVViewController.m
//  Live
//
//  Created by 熙文 张 on 16/3/3.
//  Copyright © 2016年 Heller. All rights reserved.
//

#import "LVViewController.h"
#import "UINavigationBar+LSYExtension.h"
#import "UINavigationController+LSYExtension.h"
#import "UINavigationBar+Custom.h"
#import "NSObject+YYAddForKVO.h"
#import "UIImage+YYAdd.h"
#import <objc/runtime.h>
#import "BNDOfficeDocKit.h"

@interface LVViewController()

@end

@implementation LVViewController

- (instancetype)init
{
    self = [super init];
    
    if(self) {
        self.lk_statusBarStyle = UIStatusBarStyleDefault;
        self.lk_supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
        self.modalPresentationCapturesStatusBarAppearance = YES;
    }
    
    return self;
}

- (void)dealloc
{
#ifdef DEBUG
    //统计未销毁对象 不适用首页出发的视图都不释放情况
    NSString *classStr = NSStringFromClass(self.class);
    NSLog(@"*** class [%@] dealloced", classStr);
#endif
    
    [self removeObserverBlocks];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
    }
    
    self.lv_showNavgationBar = YES;
    self.lv_navigationBarBackgroundHidden = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;
//    self.navigationController.fd_viewControllerBasedNavigationBarAppearanceEnabled = NO;
    
    [self.view setBackgroundColor:BND_VIEW_BG_COLOR];
    
    [self.navigationController.navigationBar lv_addBottomLine:UIColorWithHex(0xd7d7d7)];
    
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        UIImage *backGroundImage = [UIImage imageWithColor:UIColorWithHex(0xF6F6F8)];
        backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
        appearance.backgroundImage = backGroundImage;
        appearance.shadowColor = [UIColor clearColor];
        appearance.shadowImage = [UIImage new];
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = self.navigationController.navigationBar.standardAppearance;
    } else {
        UIImage *backGroundImage = [UIImage imageWithColor:UIColorWithHex(0xF6F6F8)];
        backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
        [self.navigationController.navigationBar setBackgroundImage:backGroundImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
    
    WS(weakSelf)
    [self addObserverBlockForKeyPath:@"needStatusBarHidden" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
        if ([oldVal boolValue] != [newVal boolValue]) {
            [weakSelf setNeedsStatusBarAppearanceUpdate];
        }
    }];
    
    [self addObserverBlockForKeyPath:@"lk_statusBarStyle" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
        if ([oldVal integerValue] != [newVal integerValue]) {
            [weakSelf setNeedsStatusBarAppearanceUpdate];
        }
    }];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:nil
                                                                  action:nil];
    self.navigationItem.backBarButtonItem = backButton;
    
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}

#pragma mark - Override

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return _lk_statusBarStyle;
}

- (BOOL)prefersStatusBarHidden
{
    return _needStatusBarHidden;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return _lk_supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

#pragma mark - Getter

- (UITableView *)createTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView setBackgroundColor:BND_VIEW_BG_COLOR];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsMake(BNDNavigationBarHeight(), 0, BNDSafeAreaBottomHeight(), 0)];
    [tableView setContentInset:(UIEdgeInsetsMake(BNDNavigationBarHeight(), 0, BNDSafeAreaBottomHeight(), 0))];
    
    return tableView;
}


#pragma mark - Setter

- (void)setLkClearBackgroundNavBar:(BOOL)lkClearBackgroundNavBar
{
    if (_lkClearBackgroundNavBar != lkClearBackgroundNavBar ||
        self.navigationController.navigationBar.translucent != lkClearBackgroundNavBar) {
        
        _lkClearBackgroundNavBar = lkClearBackgroundNavBar;
        
        if (lkClearBackgroundNavBar) {
            if (@available(iOS 13.0, *)) {
                UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
                UIImage *backGroundImage = [UIImage imageWithColor:[UIColor clearColor]];
               backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
                appearance.backgroundImage = backGroundImage;
                appearance.shadowColor = [UIColor clearColor];
                appearance.shadowImage = [UIImage new];
                [appearance setBackgroundColor:UIColor.clearColor];
                self.navigationController.navigationBar.standardAppearance = appearance;
                self.navigationController.navigationBar.scrollEdgeAppearance = nil;
                [self.navigationController.navigationBar lv_addBottomLine:UIColor.clearColor];
             
            } else {
                UIImage *backGroundImage = [UIImage imageWithColor:[UIColor clearColor]];
                backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
                [self.navigationController.navigationBar setBackgroundImage:backGroundImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
                [self.navigationController.navigationBar setShadowImage:[UIImage new]];
                [self.navigationController.navigationBar lv_addBottomLine:UIColor.clearColor];
            }
            
        }else {
            if (@available(iOS 13.0, *)) {
                UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
                UIImage *backGroundImage = [UIImage imageWithColor:UIColorWithHex(0xF6F6F8)];
                backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
                appearance.backgroundImage = backGroundImage;
                appearance.shadowColor = [UIColor clearColor];
                appearance.shadowImage = [UIImage imageWithColor:UIColorWithHex(0xd7d7d7)];
                self.navigationController.navigationBar.standardAppearance = appearance;
                self.navigationController.navigationBar.scrollEdgeAppearance = self.navigationController.navigationBar.standardAppearance;
                [self.navigationController.navigationBar lv_addBottomLine:UIColorWithHex(0xd7d7d7)];
            } else {
                UIImage *backGroundImage = [UIImage imageWithColor:UIColorWithHex(0xF6F6F8)];
                backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
                [self.navigationController.navigationBar setBackgroundImage:backGroundImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
                [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:UIColorWithHex(0xd7d7d7)]];
                [self.navigationController.navigationBar lv_addBottomLine:UIColorWithHex(0xd7d7d7)];
            }
        }
    }
}


- (void)handleNavBarStyle
{
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance setShadowImage:[[UIImage alloc] init]];
        [appearance setBackgroundColor:UIColor.whiteColor];
        UIImage *backImage = [UIImage imageNamed:@"title_icon_back"];
        [appearance setBackIndicatorImage:backImage transitionMaskImage:backImage];
        
        
        [appearance setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor]];
        [appearance setShadowImage:[UIImage imageWithColor:UIColor.whiteColor]];
        [self.navigationController.navigationBar setScrollEdgeAppearance: appearance];
    }
    
}
#pragma mark - 设置是否点击其他地方就收回键盘

@end

static const char LVViewControllerDisapperKeyboardGestureTapGesturekey;
static const char LVViewControllerDisapperKeyboardHandlekey;

@implementation LVViewController(DisapperKeyboardGesture)

- (void)addNeedDisapperKeyboardGesture
{
    [self addNeedDisapperKeyboardGestureWithHandle:nil];
}

- (void)addNeedDisapperKeyboardGestureWithHandle:(void (^)(void))handle
{
    objc_setAssociatedObject(self, &LVViewControllerDisapperKeyboardHandlekey, handle, OBJC_ASSOCIATION_RETAIN);
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureActionHandle:)];
    [tapGesture setCancelsTouchesInView:NO];
    [self.view addGestureRecognizer:tapGesture];
    objc_setAssociatedObject(self, &LVViewControllerDisapperKeyboardGestureTapGesturekey, tapGesture, OBJC_ASSOCIATION_RETAIN);
}

- (void)tapGestureActionHandle:(UITapGestureRecognizer *)tapGesture
{
    if (tapGesture.state == UIGestureRecognizerStateEnded) {
        
        void(^handle)(void) =  objc_getAssociatedObject(self, &LVViewControllerDisapperKeyboardHandlekey);
        if(handle){
            handle();
        }else{
            [self.view endEditing:YES];
        }
    }
}


@end
